﻿Imports System.IO
Imports DevExpress.XtraGrid.Views.Grid
Public Class FrmEditItem
    Dim Strsql As String
    Dim _mysql As New ClsSQLhelper
    Public ItemName As String
    Dim Flg_On_Load As Boolean = True
    Public Primary_Code As Integer
    Dim HistoryUsageID As Integer = -1
    Dim RepairationID As Integer = -1
    Public Cretiria_Option As Integer = 0
    Dim HistoryRowCount As Integer = 0
    Dim Change_Code_Status As Boolean = False

#Region "Function"
    Function Check_Percent_Lost() As Integer
        Try
            Strsql = "Select percent_lost from typeitem where Type_ID = '" & Me.TxtItemCode.Text & "'"
            Dim DT_Item As DataTable
            DT_Item = _mysql.GetMYSQLDataTable(Strsql, "items")
            If DT_Item.Rows.Count = 0 Then Exit Function
            Return DT_Item.Rows(0).Item("percent_lost").ToString
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Check_Percent_Lost"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try

    End Function
    Function Check_Usagetime() As Integer
        Try
            Strsql = "Select usagetime from typeitem where Type_ID = '" & Me.TxtItemCode.Text & "'"
            Dim DT_Item As DataTable
            DT_Item = _mysql.GetMYSQLDataTable(Strsql, "items")
            If DT_Item.Rows.Count = 0 Then Exit Function
            Return DT_Item.Rows(0).Item("usagetime").ToString
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Check_Usagetime"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try

    End Function
    Sub Load_Data()
        Try
            'Strsql = "SELECT items.Item_Type,items.Item_Code,typeitem.Type_Name FROM items left Join typeitem ON items.Item_Type = typeitem.Type_ID where Item_ID = " & Primary_Code
            'Me.lblClassName.Text = _mysql.MySQLExecuteScalar(Strsql)


            'Strsql = "select * from items where Item_Type = '" & Me.lblItemType.Text & "' and Item_Code = '/" & Me.TxtItemCode.Text & "'"
            Strsql = "Select items.*,typeitem.Type_Name from items left Join typeitem ON items.Item_Type = typeitem.Type_ID Where Item_ID = '" & Me.Primary_Code & "'"
            Dim DT_Item As DataTable
            DT_Item = _mysql.GetMYSQLDataTable(Strsql, "items")
            If DT_Item.Rows.Count = 0 Then Exit Sub
            Me.lblItemType.Text = DT_Item.Rows(0).Item("Item_Type").ToString
            Me.lblClassName.Text = DT_Item.Rows(0).Item("Type_Name").ToString

            Me.TxtItemCode.Text = DT_Item.Rows(0).Item("Item_Code").ToString
            Me.TxtBand.Text = DT_Item.Rows(0).Item("Band").ToString
            Me.TxtCompany.Text = DT_Item.Rows(0).Item("Item_From_Company").ToString
            Me.TxtDesc.Text = DT_Item.Rows(0).Item("Item_Desc").ToString
            Me.TxtDrawDocID.Text = DT_Item.Rows(0).Item("Ref_DocDrawID").ToString
            Me.TxtEngName.Text = DT_Item.Rows(0).Item("Item_EngName").ToString
            Me.TxtItemName.Text = DT_Item.Rows(0).Item("Item_Name").ToString
            Me.TxtModel.Text = DT_Item.Rows(0).Item("Item_Model").ToString
            Me.TxtPrice.Value = DT_Item.Rows(0).Item("Item_Price")
            Me.TxtRecDocID.Text = DT_Item.Rows(0).Item("Ref_DocReceiveID").ToString
            Me.TxtReceiveUser.Text = DT_Item.Rows(0).Item("Receiveby").ToString
            Me.CboBudgetYear.Text = DT_Item.Rows(0).Item("Budget_year").ToString
            Me.TxtImgpart.Text = DT_Item.Rows(0).Item("item_image_path").ToString

            Me.TxtSerialNo.Text = DT_Item.Rows(0).Item("serialno").ToString


            If String.IsNullOrEmpty(DT_Item.Rows(0).Item("Draw_Date").ToString) = False Then
                Me.DtpDrawDate.DateTime = DT_Item.Rows(0).Item("Draw_Date")
            End If

            If String.IsNullOrEmpty(DT_Item.Rows(0).Item("Receive_Date").ToString) = False Then
                Me.DtpRecDate.DateTime = DT_Item.Rows(0).Item("Receive_Date")
            End If


            Me.CboBudgetType.SelectedValue = DT_Item.Rows(0).Item("Item_Budget")
            Me.CboCountry.SelectedValue = DT_Item.Rows(0).Item("Product_From_Country")
            Me.CboDepartment.SelectedValue = DT_Item.Rows(0).Item("Department")
            Me.CboItemStatus.SelectedValue = DT_Item.Rows(0).Item("Item_Status")

            Me.ChkDischarge.Checked = IIf(DT_Item.Rows(0).Item("discharge") = "Y", True, False)

            Me.ChkNonReceiveDate.Checked = IIf(DT_Item.Rows(0).Item("no_receive_date").ToString = "Y", True, False)
            ' Me.ChkNoneDischargeDate.Checked =  IIf(DT_Item.Rows(0).Item("discharge").ToString = "Y", True, False)
            Me.ChkNonDrawDate.Checked = IIf(DT_Item.Rows(0).Item("no_draw_date").ToString = "Y", True, False)
            Chkitem_non_depreciation.Checked = IIf(DT_Item.Rows(0).Item("item_non_depreciation").ToString = "Y", True, False)

            Me.NudAge.Value = DT_Item.Rows(0).Item("usagetime")
            If NudAge.EditValue = 0 Then
                NudAge.EditValue = Check_Usagetime()
            End If

            Me.NUDPercentLost.Value = DT_Item.Rows(0).Item("lost_percent")
            Me.TxtLost.EditValue = DT_Item.Rows(0).Item("lost_cost")
            If Me.NUDPercentLost.EditValue = 0 Then
                Me.NUDPercentLost.EditValue = Check_Percent_Lost()
                If Me.TxtPrice.EditValue <> 0 Then
                    TxtLost.EditValue = (Me.TxtPrice.EditValue * Me.NUDPercentLost.EditValue) / 100
                End If
            End If

            Me.Cbopurchasetype.SelectedValue = DT_Item.Rows(0).Item("purchasetype")
            Me.ChkWaitCode.Checked = IIf(DT_Item.Rows(0).Item("wait_code") = "Y", True, False)
            If Me.ChkWaitCode.Checked = True Then
                Me.TxtItemCode.Enabled = False
                Me.PictureBox1.Visible = False
                Me.lblDuplicateStatus.Visible = False
                Me.XtraTabPage4.PageVisible = False

            Else
                Me.TxtItemCode.Enabled = True
                Me.PictureBox1.Visible = True
                Me.XtraTabPage4.PageVisible = True

                If Me.TxtItemCode.Text <> "" Then
                    lblDuplicateStatus.Visible = False
                Else
                    lblDuplicateStatus.Visible = True
                End If
            End If
            If TxtItemCode.Text <> "" Then Me.ChkWaitCode.Enabled = False Else Me.ChkWaitCode.Enabled = True
            Select Case Me.ChkDischarge.Checked
                Case True
                    'Strsql = "select * from discharge_item where Item_Type = '" & Me.lblItemType.Text & "' and Item_Code = '/" & Me.TxtItemCode.Text & "'"
                    'Dim DT_Discharge As DataTable
                    'DT_Discharge = _mysql.GetMYSQLDataTable(Strsql, "items")
                    'If DT_Discharge.Rows.Count = 0 Then Exit Sub

                    'Me.TxtDischargeBy.Text = DT_Item.Rows(0).Item("discharge_by").ToString
                    'Me.TxtDischargeRef.Text = DT_Item.Rows(0).Item("discharge_reference").ToString
                    '
                    'Me.CboDischargeBudgetYear.Text = DT_Item.Rows(0).Item("discharge_budget_year").ToString
                    'Me.CboDischargeStatus.SelectedValue = DT_Item.Rows(0).Item("discharge_reson_id")

                    ''Me.DtpDischargeDate.DateTime = DT_Item.Rows(0).Item("discharge_date")

                    'Me.ChkNonDrawDate.Checked = IIf(DT_Item.Rows(0).Item("no_discharge_date").ToString = "Y", True, False)

                    'If String.IsNullOrEmpty(DT_Item.Rows(0).Item("discharge_date").ToString) = False Then
                    '    Me.DtpDrawDate.DateTime = DT_Item.Rows(0).Item("discharge_date")
                    'End If

                    'Me.TxtDischargeValue.Value = DT_Item.Rows(0).Item("discharge_price")

                    'Me.MemoDischargeNote.Text = DT_Item.Rows(0).Item("discharge_note").ToString
                    'Load Discharge Data

                    Strsql = "Select * from discharge_item where concat(item_type,item_code) = '" & Me.lblItemType.Text & "/" & Me.TxtItemCode.Text & "'"

                    Dim DT_DischargeITem As New DataTable
                    DT_DischargeITem = _mysql.GetMYSQLDataTable(Strsql, "discharge_item")
                    If DT_DischargeITem.Rows.Count = 0 Then Exit Sub
                    Me.TxtDischargeBy.Text = DT_DischargeITem.Rows(0).Item("discharge_by")
                    Me.TxtDischargeRef.Text = DT_DischargeITem.Rows(0).Item("discharge_reference")
                    Me.ChkNoneDischargeDate.Checked = IIf(DT_DischargeITem.Rows(0).Item("no_discharge_date") = "Y", True, False)
                    If ChkNoneDischargeDate.Checked = False Then
                        Me.DtpDischargeDate.DateTime = DT_DischargeITem.Rows(0).Item("discharge_date")
                    End If
                    Me.TxtDischargeValue.Text = DT_DischargeITem.Rows(0).Item("discharge_price")
                    Me.CboDischargeBudgetYear.Text = DT_DischargeITem.Rows(0).Item("discharge_budget_year")
                    Me.CboDischargeStatus.SelectedValue = DT_DischargeITem.Rows(0).Item("discharge_reson_id")
                    Me.MemoDischargeNote.Text = DT_DischargeITem.Rows(0).Item("discharge_note")
                    ' Me.ChkPassadu.Checked = IIf(DT_DischargeITem.Rows(0).Item("passadu") = "Y", True, False)


            End Select

            If Me.TxtImgpart.Text <> "" Then
                If File.Exists(Me.TxtImgpart.Text) = True Then

                    Dim bm As New Bitmap(Me.TxtImgpart.Text)
                    PictureBox2.Image = bm
                Else
                    MsgBox("ไม่พบรูปนี้จากในระบบ ไฟล์รูปภาพอาจถูกลบ หรือเปลี่ยนที่เก็บ", MsgBoxStyle.Critical, "ไม่สามารถแสดงรูปได้")
                End If

            End If
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try

    End Sub
    Sub Load_Purchasetype()
        Try
            Strsql = "select purchase_type_id,purchase_type_name from purchasetype"
            Dim DT_PurchaseType As New DataTable

            DT_PurchaseType = _mysql.GetMYSQLDataTable(Strsql, "purchasetype")
            With Cbopurchasetype
                .DataSource = DT_PurchaseType
                .DisplayMember = "purchase_type_name"
                .ValueMember = "purchase_type_id"


            End With

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Purchasetype"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try
    End Sub
    Sub Load_BudgetYear()

        Try
            Strsql = "select min(budget_year) as year from items group by budget_year limit 1"
            Dim MinYear As Integer
            MinYear = _mysql.MySQLExecuteScalar(Strsql)

            If MinYear = 0 Then
                Me.CboBudgetYear.Properties.Items.Add(Now.Year)
            Else
                Me.CboBudgetYear.Properties.Items.Add("ไม่ระบุ")
                For i As Integer = (Now.Year + 543) To MinYear Step -1
                    Me.CboBudgetYear.Properties.Items.Add(i)
                    i = +i
                Next
            End If
            Me.CboBudgetYear.Text = (Now.Year + 543)


            CboDischargeBudgetYear.Properties.Items.Add("ไม่ระบุ")
            For i As Integer = (Now.Year + 543) To MinYear Step -1
                Me.CboDischargeBudgetYear.Properties.Items.Add(i)
                i = +i
            Next

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_BudgetYear"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try
    End Sub
    Sub Load_Budget()
        Try
            Strsql = "Select Budget_ID,Budget_Name from budget"
            Dim DT_Budget As New DataTable
            DT_Budget = _mysql.GetMYSQLDataTable(Strsql, "budget")
            With Me.CboBudgetType
                .DataSource = DT_Budget
                .DisplayMember = "Budget_Name"
                .ValueMember = "Budget_ID"
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Budget"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try

    End Sub
    Sub Load_Department()
        Try
            Strsql = "Select Department_ID,Department_Name from department  order by Department_Name"
            Dim DT_Budget As New DataTable
            DT_Budget = _mysql.GetMYSQLDataTable(Strsql, "department")
            With Me.CboDepartment
                .DataSource = DT_Budget
                .DisplayMember = "Department_Name"
                .ValueMember = "Department_ID"
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Department"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try

    End Sub
    Sub Update_Code(ByVal NewCode As String)
        Try
            Strsql = "Update items set Item_Type = '" & NewCode & "'"
            Strsql = Strsql & " where item_id='" & Me.Primary_Code & "'"
            _mysql.MySQLExecute(Strsql)

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Update_Code"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try
    End Sub
    Sub Update_Data()
        Try
            'Validate Discharge
            If Me.ChkDischarge.Checked = True Then
                If Me.TxtItemCode.Text = "" Or Me.ChkWaitCode.Checked = True Then
                    MsgBox("กรุณาใส่รหัสครุภัณฑ์ ระบบไม่สามารถแทงจำหน่ายได้หากไม่มีรหัสครุภัณฑ์กำกับ", MsgBoxStyle.Critical, "ข้ออภัยไม่สามารถแทงจำหน่ายครุภัณฑ์ได้")
                    Exit Sub
                End If
            End If


            If Change_Code_Status = True Then
                Strsql = "Update items set Item_Type = '" & Me.lblItemType.Text.Trim & "',Item_Code = '" & Me.TxtItemCode.Text.Trim & "' where Item_ID = '" & Me.Primary_Code & "'"
                _mysql.MySQLExecute(Strsql)
            End If



            'Update Data

            'Strsql = "Insert in to items (item_type,Item_Code,Item_name,Item_Desc,Item_Price,Item_Budget,Budget_year,Receive_Date,Draw_Date,Department,Ref_DocReceiveID,Ref_DocDrawID,Receiveby,Band,"
            'Strsql = "Item_EngName,Product_From_Country,Item_Model,Item_From_Company,Item_Status) "
            'Strsql = Strsql & " values ('" & frmOnHand.CboClass.SelectedValue & "','" & Me.TxtItemCode.Text & "','" & Me.TxtItemName.Text & "','" & Me.TxtDesc.Text & "'," & Me.TxtPrice.Text & ","
            'Strsql = Strsql & Me.CboBudgetType.SelectedValue & ",'" & CboBudgetYear.Text & "','" & Me.DtpRecDate.DateTime & "','" & Me.DtpDrawDate.DateTime & "'," & Me.CboDepartment.SelectedValue
            'Strsql = Strsql & ",'" & Me.TxtRecDocID.Text & "','" & Me.TxtDrawDocID.Text & "','" & Me.TxtReceiveUser.Text & "','" & Me.TxtBand.Text & "','" & Me.TxtEngName.Text & "'"
            'Strsql = Strsql & "," & Me.CboCountry.SelectedValue & ",'" & Me.TxtModel.Text & "','" & Me.TxtCompany.Text & "',0)"

            Strsql = "Update items set Item_name='" & Me.TxtItemName.Text & "',Item_Desc='" & Me.TxtDesc.Text & "',Item_Price=" & Replace(Me.TxtPrice.Text, ",", "") & ",Item_Budget=" & Me.CboBudgetType.SelectedValue & ",Budget_year='" & CboBudgetYear.Text & "'"
            Strsql = Strsql & ",Department=" & Me.CboDepartment.SelectedValue & ",Ref_DocReceiveID='" & Me.TxtRecDocID.Text & "',usagetime = " & Me.NudAge.Value & " ,purchasetype = " & Me.Cbopurchasetype.SelectedValue
            Strsql = Strsql & ",Ref_DocDrawID='" & Me.TxtDrawDocID.Text & "',Receiveby='" & Me.TxtReceiveUser.Text & "',Band='" & Me.TxtBand.Text & "'"
            Strsql = Strsql & ",Item_EngName='" & Me.TxtEngName.Text & "',Product_From_Country='" & Me.CboCountry.SelectedValue & "',Item_Model='" & Me.TxtModel.Text & "'"
            Strsql = Strsql & ",Item_From_Company='" & Me.TxtCompany.Text & "',lost_cost = " & Me.TxtLost.EditValue & ",lost_percent = " & Me.NUDPercentLost.EditValue & ",serialno='" & Me.TxtSerialNo.Text & "',Discharge = '" & IIf(Me.ChkDischarge.Checked = True, "Y", "N") & "'"
            Select Case Me.ChkNonReceiveDate.Checked
                Case True
                    Strsql = Strsql & ",Receive_Date = Null"
                Case False
                    Strsql = Strsql & ",Receive_Date='" & Pn_Framework.MysqlDateTimeFormat(Me.DtpRecDate.DateTime, False) & "'"
            End Select
            Select Case Me.ChkNonDrawDate.Checked
                Case True
                    Strsql = Strsql & ",Draw_Date = Null"
                Case False
                    Strsql = Strsql & ",Draw_Date='" & Pn_Framework.MysqlDateTimeFormat(Me.DtpDrawDate.DateTime, False) & "'"

            End Select
            Strsql = Strsql & ",no_receive_date = '" & IIf(Me.ChkNonReceiveDate.Checked = True, "Y", "N") & "'"
            Strsql = Strsql & ",no_draw_date = '" & IIf(Me.ChkNonDrawDate.Checked = True, "Y", "N") & "'"
            Strsql = Strsql & ",item_non_depreciation = '" & IIf(Me.Chkitem_non_depreciation.Checked = True, "Y", "N") & "'"
            If Me.TxtImgpart.Text = "" Then
                Strsql = Strsql & ",item_image_path = ''"

            Else
                Copy_Image2Server()
                Strsql = Strsql & ",item_image_path = '" & Replace(Me.TxtImgpart.Text, "\", "\\") & "'"
            End If

            Strsql = Strsql & ",wait_code = '" & IIf(Me.ChkWaitCode.Checked = True, "Y", "N") & "'"
            'Strsql = Strsql & " where item_type='" & frmOnHand.CboClass.SelectedValue.ToString.Trim & "' and Item_Code = '/" & Me.TxtItemCode.Text & "'"
            Strsql = Strsql & " where item_id='" & Me.Primary_Code & "'"

            If _mysql.MySQLExecute(Strsql) = 1 Then
                MsgBox("ปรับปรุงข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการปรับปรุงข้อมูล")
            Else
                'MsgBox("ไม่สามารถปรับปรุงข้อมูลได้", MsgBoxStyle.Information, "ผลการปรับปรุงข้อมูล")
                frmDebug.MemoErr_Description.Text = "ไม่สามารถปรับปรุงข้อมูลได้ กรุณาตรวจสอบคำสั่ง SQL"
                frmDebug.MemoSQL.Text = Strsql
                frmDebug.ShowDialog()
            End If

            'Add Log
            Strsql = "Insert into transections_logs (trans_code,trans_datetime,trans_computer,trans_item) values (2,'" & Pn_Framework.MysqlDateTimeFormat(Now, True) & "','" & Pn_Framework.Get_Computername & "','" & Me.Primary_Code & "') "
            Select Case (_mysql.MySQLExecute(Strsql))
                Case -1
                    frmDebug.lblFormName.Text = Me.Name
                    frmDebug.lblFunctionName.Text = "Update_Data"
                    frmDebug.MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                    frmDebug.MemoSQL.Text = Strsql
                    frmDebug.ShowDialog()
            End Select

            Select Case Me.ChkDischarge.Checked
                Case True
                    Strsql = "Select Count(*) as cc from discharge_item where concat(item_type,item_code) = '" & Me.lblItemType.Text & "/" & Me.TxtItemCode.Text & "'"
                    Dim DischargeRecord As Integer = 0
                    DischargeRecord = _mysql.MySQLExecuteScalar(Strsql)
                    Select Case DischargeRecord
                        Case 0
                            Strsql = "Insert into discharge_item (item_type,item_code,discharge_budget_year,no_discharge_date,discharge_reson_id"
                            Strsql = Strsql & ",discharge_by,discharge_note,discharge_reference,discharge_price,discharge_date) value "
                            Strsql = Strsql & "('" & Me.lblItemType.Text & "','/" & Me.TxtItemCode.Text & "'"
                            Strsql = Strsql & ",'" & Me.CboDischargeBudgetYear.Text & "','" & IIf(Me.ChkNoneDischargeDate.Checked = True, "Y", "N") & "',"
                            Strsql = Strsql & Me.CboDischargeStatus.SelectedValue & ",'" & Me.TxtDischargeBy.Text & "','" & Me.MemoDischargeNote.Text & "'"
                            Strsql = Strsql & ",'" & Me.TxtDischargeRef.Text & "'," & Replace(Me.TxtDischargeValue.Text, ",", "")
                            Select Case Me.ChkNoneDischargeDate.Checked
                                Case True
                                    Strsql = Strsql & ",NULL"
                                Case False
                                    Strsql = Strsql & ",'" & Pn_Framework.MysqlDateTimeFormat(Me.DtpDischargeDate.DateTime, False) & "'"
                            End Select
                            Strsql = Strsql & ")"
                            _mysql.MySQLExecute(Strsql)
                        Case Is > 0
                            Strsql = "Update discharge_item set discharge_budget_year = '" & Me.CboDischargeBudgetYear.Text & "' "
                            Strsql = Strsql & ",no_discharge_date='" & IIf(Me.ChkNoneDischargeDate.Checked = True, "Y", "N") & "'"

                            Select Case Me.ChkNoneDischargeDate.Checked
                                Case True
                                    Strsql = Strsql & ",discharge_date=NULL"
                                Case False
                                    Strsql = Strsql & ",discharge_date='" & Pn_Framework.MysqlDateTimeFormat(Me.DtpDischargeDate.DateTime, False) & "'"
                            End Select
                            Strsql = Strsql & ",discharge_reson_id=" & Me.CboDischargeStatus.SelectedValue
                            Strsql = Strsql & ",discharge_by= '" & Me.TxtDischargeBy.Text & "'"
                            Strsql = Strsql & ",discharge_note='" & Me.MemoDischargeNote.Text & "'"
                            Strsql = Strsql & ",discharge_reference='" & Me.TxtDischargeRef.Text & "'"
                            Strsql = Strsql & ",discharge_price=" & Replace(Me.TxtDischargeValue.Text, ",", "")
                            Strsql = Strsql & " where concat(item_type,item_code) =  '" & Me.lblItemType.Text & "/" & Me.TxtItemCode.Text & "'"
                            _mysql.MySQLExecute(Strsql)
                    End Select
                Case False
                    Strsql = "Delete from discharge_item where concat(item_type,item_code) =  '" & Me.lblItemType.Text & "/" & Me.TxtItemCode.Text & "'"
                    _mysql.MySQLExecute(Strsql)
            End Select


            'Add Log
            Strsql = "Insert into transections_logs (trans_code,trans_datetime,trans_computer,trans_item) values (4,'" & Pn_Framework.MysqlDateTimeFormat(Now, True) & "','" & Pn_Framework.Get_Computername & "','" & Me.Primary_Code & "') "
            Select Case (_mysql.MySQLExecute(Strsql))
                Case -1
                    frmDebug.lblFormName.Text = Me.Name
                    frmDebug.lblFunctionName.Text = "Update_Data"
                    frmDebug.MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                    frmDebug.MemoSQL.Text = Strsql
                    frmDebug.ShowDialog()
            End Select

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Update_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try
    End Sub
    Sub Load_ItemStatus()
        Try
            Strsql = "Select Status_ID,Status_Name from itemstatus"
            Dim DT_ItemStatus As New DataTable
            DT_ItemStatus = _mysql.GetMYSQLDataTable(Strsql, "itemstatus")
            With Me.CboItemStatus
                .DataSource = DT_ItemStatus
                .DisplayMember = "Status_Name"
                .ValueMember = "Status_ID"
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_ItemStatus"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try

    End Sub

    Function Lock_Control(ByVal Enable As Boolean)
        Try

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Function
    Sub Load_Nationality()
        Try
            Strsql = "select * from nationality order by nationality_ID"
            Dim DT_Nationality As DataTable
            DT_Nationality = _mysql.GetMYSQLDataTable(Strsql, "Nationality")
            With Me.CboCountry
                .DataSource = DT_Nationality
                .DisplayMember = "nationality_Name"
                .ValueMember = "nationality_ID"
            End With

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Nationality"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try
    End Sub
    Sub Load_Discharge_Reson()
        Try
            Strsql = "Select discharge_reson_id,discharge_reson_name from discharge_reson"
            Dim DT_Budget As New DataTable
            DT_Budget = _mysql.GetMYSQLDataTable(Strsql, "budget")
            With Me.CboDischargeStatus
                .DataSource = DT_Budget
                .DisplayMember = "discharge_reson_name"
                .ValueMember = "discharge_reson_id"
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Discharge_Reson"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try
    End Sub

    Sub Copy_Image2Server()
        Dim StrFilename As String = ""
        Try

            StrFilename = My.Settings.ImgSharePart & "\" & Me.lblItemType.Text & "-" & Replace(Me.TxtItemCode.Text, "/", "") & " " & Now.Year.ToString & Now.Month.ToString & Now.Day.ToString & Now.Hour & Now.Minute & Now.Second & ".jpg"

            File.Copy(Me.TxtImgpart.Text, StrFilename, True)
            Dim bm As New Bitmap(Me.TxtImgpart.Text)
            PictureBox2.Image = bm
            Me.TxtImgpart.Text = StrFilename
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Copy_Image2Server"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = "StrFilename = " & StrFilename
                .ShowDialog()
            End With
        End Try

    End Sub

    Sub Load_AutoComplete_Item_Name()
        Try
            Strsql = "select Item_Name from items WHERE items.Item_Name IS NOT NULL  AND items.Item_Name <>  '' AND items.Item_Type =  '" & lblClassName.Text & "' group by Item_Name"
            Dim DT_ItemName As New DataTable
            DT_ItemName = _mysql.GetMYSQLDataTable(Strsql, "items")
            Dim ColItemName As New AutoCompleteStringCollection
            For i = 0 To DT_ItemName.Rows.Count - 1
                ColItemName.Add(DT_ItemName.Rows(i).Item("Item_Name"))
            Next
            Me.TxtItemName.AutoCompleteCustomSource = ColItemName
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_AutoComplete_Item_Name"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try
    End Sub
    Sub Load_AutoComplete_Item_Band()
        Try
            Strsql = "select Band from items where Band is not null and Band <> '' group by Band"
            Dim DT_ItemName As New DataTable
            DT_ItemName = _mysql.GetMYSQLDataTable(Strsql, "items")
            Dim ColItemName As New AutoCompleteStringCollection
            For i = 0 To DT_ItemName.Rows.Count - 1
                ColItemName.Add(DT_ItemName.Rows(i).Item("Band"))
            Next
            Me.TxtBand.AutoCompleteCustomSource = ColItemName
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_AutoComplete_Item_Band"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try
    End Sub
    Sub Load_AutoComplete_Item_EngName()
        Try
            Strsql = "select Item_EngName from items where Item_EngName is not null and  Item_EngName <> '' group by Item_EngName"
            Dim DT_ItemName As New DataTable
            DT_ItemName = _mysql.GetMYSQLDataTable(Strsql, "items")
            Dim ColItemName As New AutoCompleteStringCollection
            For i = 0 To DT_ItemName.Rows.Count - 1
                ColItemName.Add(DT_ItemName.Rows(i).Item("Item_EngName"))
            Next
            Me.TxtEngName.AutoCompleteCustomSource = ColItemName
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_AutoComplete_Item_EngName"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try
    End Sub
    Sub Load_AutoComplete_Item_Model()
        Try
            Strsql = "select Item_Model from items where Item_Model is not null and  Item_Model <> ''   group by Item_Model"
            Dim DT_ItemName As New DataTable
            DT_ItemName = _mysql.GetMYSQLDataTable(Strsql, "items")
            Dim ColItemName As New AutoCompleteStringCollection
            For i = 0 To DT_ItemName.Rows.Count - 1
                ColItemName.Add(DT_ItemName.Rows(i).Item("Item_Model"))
            Next
            Me.TxtModel.AutoCompleteCustomSource = ColItemName
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_AutoComplete_Item_Model"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try
    End Sub
    Sub Load_AutoComplete_Item_From_Company()
        Try
            Strsql = "select Item_From_Company from items where Item_From_Company is not null and Item_From_Company <> '' group by Item_From_Company"
            Dim DT_ItemName As New DataTable
            DT_ItemName = _mysql.GetMYSQLDataTable(Strsql, "items")
            Dim ColItemName As New AutoCompleteStringCollection
            For i = 0 To DT_ItemName.Rows.Count - 1
                ColItemName.Add(DT_ItemName.Rows(i).Item("Item_From_Company"))
            Next
            Me.TxtCompany.AutoCompleteCustomSource = ColItemName
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_AutoComplete_Item_From_Company"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try
    End Sub
    Sub Load_AutoComplete_Receivername()
        Try
            Strsql = "select receiveby from items where receiveby is not null and receiveby <> '' group by receiveby"
            Dim DT_ItemName As New DataTable
            DT_ItemName = _mysql.GetMYSQLDataTable(Strsql, "items")
            Dim ColItemName As New AutoCompleteStringCollection
            For i = 0 To DT_ItemName.Rows.Count - 1
                ColItemName.Add(DT_ItemName.Rows(i).Item("receiveby"))
            Next
            Me.TxtReceiveUser.AutoCompleteCustomSource = ColItemName
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_AutoComplete_Receivername"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try
    End Sub
    Function Validate_Data() As Boolean
        Validate_Data = False
        Try
            If Me.ChkWaitCode.Checked <> True Then
                If Me.TxtItemCode.Text = "" Then
                    MsgBox("กรุณาใส่รหัสครุภัณฑ์", MsgBoxStyle.Exclamation, "ไม่สามารถบันทึกได้")
                    Exit Function
                End If
            End If

            If Me.TxtItemName.Text = "" Then
                MsgBox("กรุณาใส่ชื่อครุภัณฑ์", MsgBoxStyle.Exclamation, "ไม่สามารถบันทึกได้")
                Exit Function
            End If
            Validate_Data = True
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Validate_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try
    End Function
    Function Validate_Code() As Boolean
        Validate_Code = False
        Try

            Dim ItemCode As String = ""
            ItemCode = Replace(Me.TxtItemCode.Text, "_", "")


            If ItemCode.Length < 4 Then
                PictureBox1.Image = WHAS.My.Resources.Resources.delete
                lblDuplicateStatus.Text = "คุณไม่สามารถใช้รหัสนี้ได้"
                lblDuplicateStatus.ForeColor = Color.Red
                Me.BtnSave.Enabled = False
                Exit Function
            End If

            Strsql = "select count(*) from items where  item_type = '" & Me.lblItemType.Text & "' and Item_Code = '" & Me.TxtItemCode.Text & "'"
            Dim DuplicateItem As Integer = 0
            DuplicateItem = _mysql.MySQLExecuteScalar(Strsql)
            If DuplicateItem = 0 Then
                PictureBox1.Image = WHAS.My.Resources.Resources.check
                lblDuplicateStatus.Text = "คุณสามารถใช้รหัสนี้ได้"
                lblDuplicateStatus.ForeColor = Color.Green
                Me.BtnSave.Enabled = True
                Validate_Code = True
            Else
                PictureBox1.Image = WHAS.My.Resources.Resources.delete
                lblDuplicateStatus.Text = "คุณไม่สามารถใช้รหัสนี้ได้"
                lblDuplicateStatus.ForeColor = Color.Red
                BtnSave.Enabled = False
                '(MsgBox("ไม่สามารถใช้หมายเลขคุรุภัณฑ์นี้ได้", MsgBoxStyle.Exclamation, "คำเตือน"))
            End If
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Validate_Code"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try
    End Function
    Sub Load_HistoryUsage()
        Try
            Strsql = "select historyusage_id,historyusage_itemid,department.Department_Name,historyusage_startdate,historyusage_remarks "
            Strsql = Strsql & " from historyusage left Join department ON   historyusage.historyusage_department = department.Department_ID where historyusage_itemid = " & Primary_Code & " order by historyusage_id desc"
            Dim DT_HTUsage As New DataTable
            DT_HTUsage = _mysql.GetMYSQLDataTable(Strsql, "historyusage")
            If DT_HTUsage.Rows.Count > 0 Then
                HistoryUsageID = DT_HTUsage.Rows(0).Item("historyusage_id")
            End If
            With gridhistory
                .DataSource = DT_HTUsage
                GridColumn1.FieldName = "historyusage_id"
                GridColumn1.Caption = "เลขที่"



                GridColumn2.FieldName = "historyusage_itemid"
                GridColumn2.Caption = "เลขที่ครุภัณฑ์"
                GridColumn2.Visible = False

                GridColumn3.FieldName = "Department_Name"
                GridColumn3.Caption = "แผนก"

                GridColumn4.FieldName = "historyusage_startdate"
                GridColumn4.Caption = "วันที่เริ่มใช้"

                'GridColumn5.FieldName = "historyusage_enddate"
                'GridColumn5.Caption = "วันที่เลิกใช้"


                GridColumn6.FieldName = "historyusage_remarks"
                GridColumn6.Caption = "หมายเหตุ"



            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Validate_Code"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try
    End Sub
    Sub Load_Repairation()
        Try
            Strsql = "select repair_id,item_id,repair_place,repair_date,return_date,repair_status,repair_status_name,remark "
            Strsql = Strsql & " from repairation left join repair_status on  repairation.repair_status = repair_status.repair_status_id   where item_id = " & Primary_Code & " order by item_id desc"
            Dim DT_Repairation As New DataTable
            DT_Repairation = _mysql.GetMYSQLDataTable(Strsql, "repairation")
            If DT_Repairation.Rows.Count > 0 Then
                HistoryUsageID = DT_Repairation.Rows(0).Item("repair_id")
            End If
            With gridRepairation
                .DataSource = DT_Repairation
                GridColumn7.FieldName = "repair_id"
                GridColumn7.Caption = "เลขที่"

                GridColumn8.FieldName = "item_id"
                GridColumn8.Caption = "เลขที่ครุภัณฑ์"
                GridColumn8.Visible = False

                GridColumn9.FieldName = "repair_place"
                GridColumn9.Caption = "สถานที่ส่งซ่อม"

                GridColumn10.FieldName = "repair_date"
                GridColumn10.Caption = "วันที่ส่งซ่อม"

                GridColumn11.FieldName = "return_date"
                GridColumn11.Caption = "วันที่ได้คืน"

                GridColumn12.FieldName = "repair_status_name"
                GridColumn12.Caption = "สถาณะการซ่อม"

                GridColumn13.FieldName = "remark"
                GridColumn13.Caption = "หมายเหตุ"
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Validate_Code"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try
    End Sub
 
#End Region


    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Dispose()
    End Sub

    Private Sub FrmEditItem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Budget()
        Load_Department()
        Load_BudgetYear()
        Load_Nationality()
        Load_ItemStatus()
        Load_Discharge_Reson()
        Load_Purchasetype()
        Load_Data()
        Load_HistoryUsage()
        Load_Repairation()

        'Load Autocomplete
        Load_AutoComplete_Item_Name()
        Load_AutoComplete_Item_Band()
        Load_AutoComplete_Item_EngName()
        Load_AutoComplete_Item_Model()
        Load_AutoComplete_Item_From_Company()
        Load_AutoComplete_Receivername()
        Flg_On_Load = False
    End Sub



    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        If MsgBox("คุณต้องการบันทึกข้อมูลใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยัน") = MsgBoxResult.No Then Exit Sub
        If Validate_Data() = False Then Exit Sub
        Update_Data()
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Dispose()
    End Sub

    Private Sub ChkNonReceiveDate_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkNonReceiveDate.CheckedChanged

    End Sub

    Private Sub ChkDischarge_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkDischarge.CheckedChanged
        
        Select Case Me.ChkDischarge.Checked
            Case True
                Me.CboDischargeStatus.Enabled = True
                Me.CboDischargeBudgetYear.Enabled = True
                Me.TxtDischargeBy.Enabled = True
                Me.TxtDischargeRef.Enabled = True
                Me.TxtDischargeValue.Enabled = True
                Me.MemoDischargeNote.Enabled = True
                Me.DtpDischargeDate.Enabled = True
                Me.ChkNoneDischargeDate.Enabled = True

            Case False
                Me.CboDischargeStatus.Enabled = False
                Me.CboDischargeBudgetYear.Enabled = False
                Me.TxtDischargeBy.Enabled = False
                Me.TxtDischargeRef.Enabled = False
                Me.TxtDischargeValue.Enabled = False
                Me.MemoDischargeNote.Enabled = False
                Me.DtpDischargeDate.Enabled = False
                Me.ChkNoneDischargeDate.Enabled = False

        End Select
    End Sub

    Private Sub CboItemStatus_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboItemStatus.SelectedIndexChanged
        With CboItemStatus
            'If .SelectedValue = 3 Then
            '    XtraTabControl1.SelectedTabPage = XtraTabPage4

            'End If
        End With
    End Sub

    Private Sub PicLoadImg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PicLoadImg.Click
        Try
            With Me.OpenFileDialog1
                .Title = "เลือกรูปภาพ"
                .FileName = ""
                .Filter = "Jpeg Image|*.jpg"
                If .ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub

                Me.TxtImgpart.Text = .FileName
                Dim bm As New Bitmap(Me.TxtImgpart.Text)
                PictureBox2.Image = bm

            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "PicLoadImg_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub

    Private Sub PictureBox6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox6.Click
        Frmcapture.StrItemName = Me.lblItemType.Text & "-" & Me.TxtItemCode.Text
        Frmcapture.ShowDialog()
        If Frmcapture.ImagePart = "" Then Exit Sub
        'Me.TxtImgpart.Text = Frmcapture.MyCapturepart
        Me.TxtImgpart.Text = Frmcapture.sfdImage.FileName
        Dim bm As New Bitmap(Me.TxtImgpart.Text)
        PictureBox2.Image = bm
        Frmcapture.Dispose()
    End Sub

    Private Sub picZoomin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picZoomin.Click
        'FrmFullSize.PictureEdit1
        FrmFullSize.PictureEdit1.Image = Me.PictureBox2.Image
        FrmFullSize.Show()
    End Sub

    Private Sub XtraTabControl1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles XtraTabControl1.Click

    End Sub

    Private Sub picClearimg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picClearimg.Click
        Try
            PictureBox1.Image = Nothing
            Me.TxtImgpart.Text = ""
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub ChkWaitCode_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkWaitCode.CheckedChanged
        If ChkWaitCode.Checked = True Then
            Me.TxtItemCode.Text = ""
            Me.lblDuplicateStatus.Visible = False

            Me.PictureBox1.Visible = False
            Me.TxtItemCode.Enabled = False


        Else
            Me.lblDuplicateStatus.Visible = True
            Me.PictureBox1.Visible = True
            Me.TxtItemCode.Enabled = True

            Me.TxtItemCode.Focus()
        End If
    End Sub



    Private Sub TxtItemCode_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtItemCode.EditValueChanged
        If Flg_On_Load = True Then Exit Sub
        If Me.TxtItemCode.EditValue = "" Then
            Me.ChkWaitCode.Enabled = True

            Exit Sub
        Else
            Me.ChkWaitCode.Enabled = False
        End If

        Validate_Data()
    End Sub

    Private Sub GridControl1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub


    Private Sub btnAddHistory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddHistory.Click
        frmNewUsageHistory.Primary_code = Me.Primary_Code
        If frmNewUsageHistory.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub
        Load_HistoryUsage()
    End Sub

    Private Sub btnEditHistory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditHistory.Click
        If HistoryUsageID = -1 Then Exit Sub
        frmEditUsageHistory.Primary_code = Me.HistoryUsageID
        If frmEditUsageHistory.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub
        Load_HistoryUsage()
    End Sub

    Private Sub MnAddRepair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnAddRepair.Click
        frmNewRepairation.Primary_code = Me.Primary_Code
        If frmNewRepairation.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub
        Load_Repairation()
    End Sub

    Private Sub MnEditRepair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnEditRepair.Click
        If Me.RepairationID = -1 Then Exit Sub
        frmEditRepairation.Primary_code = Me.RepairationID
        If frmEditRepairation.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub
        Load_Repairation()
    End Sub

    Private Sub GridView1_CustomRowCellEditForEditing(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) Handles GridView1.CustomRowCellEditForEditing
        Try
            ' Load_HistoryUsage()
            Dim Gv As GridView = sender
            HistoryUsageID = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("historyusage_id")).ToString()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub BtnDeleteHistory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDeleteHistory.Click
        Try
            'Check Bug No record in Grid
            Dim HistoryGv As GridView = gridhistory.MainView

            If HistoryGv.RowCount = 0 Then Exit Sub


            If HistoryUsageID = -1 Then Exit Sub
            If (MsgBox("กรุณายืนยันการลบข้อมูล " & Primary_Code, MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบข้อมูล") = MsgBoxResult.No) Then Exit Sub
            If (MsgBox("การลบข้อมูลเมื่อยืนยันแล้วจะไม่สามารถนำกลับมาใหม่ได้ คุณยังต้องการยืนยันใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบข้อมูล") = MsgBoxResult.No) Then Exit Sub
            Strsql = "Delete from historyusage where historyusage_id = " & HistoryUsageID
            If _mysql.MySQLExecute(Strsql) = -1 Then
                With frmDebug
                    .lblFormName.Text = Me.Name
                    .lblFunctionName.Text = "BtnDeleteHistory_Click"
                    .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                    .MemoSQL.Text = Strsql
                    .ShowDialog()

                End With
            End If
            Load_HistoryUsage()
            HistoryUsageID = -1
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Validate_Code"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try
    End Sub

    Private Sub MnDeleteRepair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnDeleteRepair.Click
        Try
            Dim RepairGv As GridView = gridRepairation.MainView
            If RepairGv.RowCount = 0 Then Exit Sub

            If RepairationID = -1 Then Exit Sub
            If (MsgBox("กรุณายืนยันการลบข้อมูล " & RepairationID, MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบข้อมูล") = MsgBoxResult.No) Then Exit Sub
            If (MsgBox("การลบข้อมูลเมื่อยืนยันแล้วจะไม่สามารถนำกลับมาใหม่ได้ คุณยังต้องการยืนยันใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบข้อมูล") = MsgBoxResult.No) Then Exit Sub
            Strsql = "Delete from repairation where repair_id = " & RepairationID
            If _mysql.MySQLExecute(Strsql) = -1 Then
                With frmDebug
                    .lblFormName.Text = Me.Name
                    .lblFunctionName.Text = "BtnDeleteHistory_Click"
                    .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                    .MemoSQL.Text = Strsql
                    .ShowDialog()

                End With
            End If
            Load_Repairation()
            RepairationID = -1

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Validate_Code"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try
    End Sub

    Private Sub gridRepairation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gridRepairation.Click

    End Sub

    Private Sub GridView3_CustomRowCellEditForEditing(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) Handles GridView3.CustomRowCellEditForEditing
        Try
            'Load_Repairation()
            Dim Gv As GridView = sender
            RepairationID = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("repair_id")).ToString()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub gridhistory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gridhistory.Click

    End Sub

    Private Sub TxtItemCode_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtItemCode.LostFocus
       
    End Sub

    Private Sub BtnChangeCode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnChangeCode.Click
        Try
            If frmChangeCode.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Dim NewCode As String = frmChangeCode.TxtNewCode.Text
                frmChangeCode.Dispose()
                Strsql = "Select Count(*) as cc from items where Item_Type = '" & NewCode & "' and Item_Code = '" & Me.TxtItemCode.Text & "'"
                Dim ExistCheckRow As Integer = _mysql.MySQLExecuteScalar(Strsql)
                If ExistCheckRow > 0 Then
                    MsgBox("รหัสนี้ถูกใช้แล้วไม่สามารถใช้ได้", MsgBoxStyle.Critical, "ไม่สามารถดำเนินการได้")
                Else
                    Me.lblItemType.Text = NewCode
                    Strsql = "Select Type_Name from typeitem where Type_ID = '" & NewCode & "'"
                    lblClassName.Text = ": " & _mysql.MySQLExecuteScalar(Strsql)
                    Change_Code_Status = True


                End If
            End If

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnChangeCode_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With
        End Try

    End Sub

    Private Sub TxtPrice_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtPrice.EditValueChanged
        If TxtPrice.EditValue > 5000 Then
            Me.Chkitem_non_depreciation.Checked = False
        Else
            Me.Chkitem_non_depreciation.Checked = True
        End If

        If TxtPrice.EditValue <= 100000 Then
            Me.Cbopurchasetype.SelectedValue = 1
        End If
        If TxtPrice.EditValue > 100000 And TxtPrice.EditValue <= 2000000 Then
            Me.Cbopurchasetype.SelectedValue = 2
        End If


        If TxtPrice.EditValue > 2000000 Then
            Me.Cbopurchasetype.SelectedValue = 3
        End If
        If Me.TxtPrice.EditValue <> 0 Then
            TxtLost.EditValue = (Me.TxtPrice.EditValue * Me.NUDPercentLost.EditValue) / 100
        End If
    End Sub

    Private Sub NUDPercentLost_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NUDPercentLost.EditValueChanged
        If Me.TxtPrice.EditValue <> 0 Then
            TxtLost.EditValue = (Me.TxtPrice.EditValue * Me.NUDPercentLost.EditValue) / 100
        End If


    End Sub
End Class